<template>
  <div class="custom-select" :tabindex="0" @blur="isOpen = false">
    <div class="selected-option" @click="toggleDropdown">
      <span>{{ selectedOptionText || placeholder }}</span>
      <span class="dropdown-arrow" :class="{ 'open': isOpen }">&#xE70E;</span>
    </div>
    <transition name="fade">
      <div v-if="isOpen" class="options-list" ref="optionsList">
        <ul>
          <li 
            v-for="option in options" 
            :key="option.value" 
            @click="selectOption(option)"
            :class="{ 'selected': option.value === value }"
          >
            {{ option.text }}
          </li>
        </ul>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'CustomSelect',
  props: {
    options: {
      type: Array,
      required: true,
      default: () => [] // e.g., [{ value: 'val', text: 'Label' }]
    },
    value: { // for v-model
      type: [String, Number],
      default: null
    },
    placeholder: {
      type: String,
      default: '请选择'
    }
  },
  data() {
    return {
      isOpen: false,
    };
  },
  computed: {
    selectedOptionText() {
      const selected = this.options.find(option => option.value === this.value);
      return selected ? selected.text : '';
    }
  },
  methods: {
    toggleDropdown() {
      this.isOpen = !this.isOpen;
    },
    selectOption(option) {
      this.$emit('input', option.value); // for v-model
      this.isOpen = false;
    }
  },
  directives: {
    // A simple click-outside directive could be added here if needed,
    // but for now, using @blur on the root element for simplicity.
  }
};
</script>

<style scoped>
.custom-select {
  position: relative;
  width: 100%;
  font-family: var(--font-family-base);
  outline: none; /* Handled by focus style on .selected-option */
}

.selected-option {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--spacing-xs) var(--spacing-md);
  font-size: var(--font-size-base);
  color: var(--theme-text-input);
  background-color: var(--theme-bg-input);
  border: var(--border-width) var(--border-style) var(--theme-border-input);
  border-radius: var(--border-radius-base);
  cursor: pointer;
  transition: border-color var(--transition-duration-short), box-shadow var(--transition-duration-short);
  user-select: none;
}

.selected-option:hover {
  border-color: var(--theme-border-secondary); /* Or a more specific input hover border */
}

.custom-select:focus .selected-option,
.custom-select.focus-within .selected-option { 
  border-color: var(--theme-accent-primary);
  box-shadow: 0 0 0 3px rgba(var(--theme-accent-primary-rgb), 0.3);
}

.dropdown-arrow {
  font-family: var(--font-family-icons);
  font-size: var(--font-size-base);
  color: var(--theme-text-muted);
  transition: transform var(--transition-duration-base) ease-in-out;
}

.dropdown-arrow.open {
  transform: rotate(180deg);
}

.options-list {
  position: absolute;
  top: calc(100% + var(--spacing-xs));
  left: 0;
  right: 0;
  background-color: var(--theme-bg-tertiary);
  border: var(--border-width) var(--border-style) var(--theme-border-secondary);
  border-radius: var(--border-radius-base);
  box-shadow: var(--theme-shadow-popover);
  z-index: 1050;
  max-height: 150px;
  overflow-y: auto;
}

.options-list ul {
  list-style: none;
  padding: var(--spacing-sm) 0;
  margin: 0;
}

.options-list li {
  padding: var(--spacing-sm) var(--spacing-md);
  font-size: var(--font-size-base);
  color: var(--theme-text-secondary);
  cursor: pointer;
  transition: background-color var(--transition-duration-short), color var(--transition-duration-short);
  user-select: none;
}

.options-list li:hover {
  background-color: var(--theme-bg-hover);
  color: var(--theme-text-primary);
}

.options-list li.selected {
  background: var(--theme-accent-primary); /* Using solid accent for selected item */
  color: var(--theme-text-on-accent);
  font-weight: var(--font-weight-medium);
}

/* Scrollbar for options list */
.options-list::-webkit-scrollbar {
  width: 6px;
}
.options-list::-webkit-scrollbar-track {
  background: transparent; /* Or var(--theme-bg-tertiary) to match list bg */
}
.options-list::-webkit-scrollbar-thumb {
  background-color: var(--theme-scrollbar-thumb-bg);
  border-radius: var(--border-radius-base); /* Consistent radius */
}
.options-list::-webkit-scrollbar-thumb:hover {
  background-color: var(--theme-scrollbar-thumb-hover-bg);
}


/* Fade transition for the options list */
.fade-enter-active,
.fade-leave-active {
  transition: opacity var(--transition-duration-base) ease, transform var(--transition-duration-base) ease;
  transform-origin: top center;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
  transform: scaleY(0.95) translateY(-10px);
}
</style> 